package com.lpit.mysql;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import lombok.Data;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
import java.io.IOException;
import java.util.Properties;

@Data
@MapperScan(basePackages = "com.lpit.mapper", sqlSessionFactoryRef = "mysqlDevSqlSession")
@Configuration
@PropertySource("classpath:mysql-dev-jdbc.properties")//地址
@ConfigurationProperties(prefix = "mysql.dev.jdbc")
public class MysqlConfig {
    private String driver;
    private String url;
    private String username;
    private String password;
    private String aliasesPackage;
    private String rootMapper;

    @Bean("mysqlDevDataSource")
    public DataSource mysqlDevDataSource() throws Exception {
        Properties properties = new Properties();
        properties.setProperty("driverClassName", this.getDriver());
        properties.setProperty("url", this.getUrl());
        properties.setProperty("username", this.getUsername());
        properties.setProperty("password", this.getPassword());
        return DruidDataSourceFactory.createDataSource(properties);
    }


    @Bean("mysqlDevSqlSession")
    public MybatisSqlSessionFactoryBean mysqlDevSqlSession(@Qualifier("mysqlDevDataSource") DataSource dataSource) throws IOException {
        System.out.println(this);

        /*配置生产SqlSession对象的工厂*/
        MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        //开启分页
        sqlSessionFactoryBean.setPlugins(mybatisPlusInterceptor());
        /*注入数据源*/
        sqlSessionFactoryBean.setDataSource(dataSource);
        /*给包下所有对象起别名*/
        sqlSessionFactoryBean.setTypeAliasesPackage(this.getAliasesPackage());

        /*创建解析文件资源路径类，并将需要解析的路径getResources*/
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        Resource[] resource = resolver.getResources(this.getMapperFilePath());
        sqlSessionFactoryBean.setMapperLocations(resource);

        /*mysql配置类*/
        MybatisConfiguration mybatisConfiguration = new MybatisConfiguration();
        /*开启驼峰命名*/
        mybatisConfiguration.setMapUnderscoreToCamelCase(true);
        /*打印sql日志*/
        mybatisConfiguration.setLogImpl(StdOutImpl.class);

        sqlSessionFactoryBean.setConfiguration(mybatisConfiguration);



        return sqlSessionFactoryBean;

    }

    public String getMapperFilePath() {
        return "classpath:" + this.getRootMapper() + "**/*.xml";
    }

    /**
     * 分页工具
     * @return
     */
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        //添加分页插件
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}
